package cn.blogger.service;

import cn.blogger.dao.CommentDao;
import cn.blogger.pojo.TBlog;
import cn.blogger.pojo.TComment;
import cn.blogger.pojo.TUser;
import cn.blogger.vo.CommentVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service(interfaceClass = CommenService.class)
@Transactional
public class CommenServiceImpl implements CommenService{
    @Autowired
    private CommentDao commentDao;


    @Override
    public List<TComment> queryComment(CommentVO commentVO) {
        return commentDao.commentListQuery(commentVO);
    }

    @Override
    public Integer addComment(TComment comment) {
        Integer toUid = comment.getTo_uid();
        Integer firstCommentId = null;
        if(toUid != null){
            firstCommentId = commentDao.firstCommentIdQuery(toUid);
            if(firstCommentId == null)  firstCommentId = toUid;
        }
        comment.setFirst_comment_uid(firstCommentId);
        return commentDao.addComment(comment);
    }

    @Override
    public void deleteComment(Integer uid) {
        //因为uid是主键，所以最多查出一个顶级评论
        CommentVO commentVO = new CommentVO();
        commentVO.setUid(uid);
        List<TComment> comments =commentDao.commentListQuery(commentVO);
        if(comments.size() == 0) return;
        TComment cur = comments.get(0);
        //查出该评论下所有评论
        List<Integer> commentsToDelete = new ArrayList<>();
        LinkedList<TComment> deque = new LinkedList<>();
        if(cur != null) deque.offer(cur);
        while(!deque.isEmpty()){
            int size = deque.size();
            for(int i = 0; i < size; i++){
                cur = deque.poll();
                commentsToDelete.add(cur.getUid());
                System.out.println(cur.getUid());
                for(TComment comment :cur.getReplyList()){
                    deque.offer(comment);
                }
            }
        }

        commentDao.deleteComment(commentsToDelete);
    }

    @Override
    public List<TComment>  queryCommentByCondition(CommentVO commentVO) {
        PageHelper.startPage(commentVO.getCurrentPage(),commentVO.getPageSize());
        List<TComment> comments = commentDao.commentListQuery(commentVO);
        PageInfo<TComment> pageInfo = new PageInfo<>(comments);
        return pageInfo.getList();
    }

    @Override
    public List<TComment> listAll(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<TComment> comments = commentDao.commentListAll();
        PageInfo<TComment> pageInfo = new PageInfo<>(comments);
        return pageInfo.getList();
    }

    @Override
    public void userCommentDelete(Integer userUid) {
        //查出用户所有评论的uid
       List<Integer> commentsId = commentDao.userCommentQuery(userUid);
       //逐条删除
       for(Integer commentId:commentsId){
           //本类的方法
           deleteComment(commentId);
       }
    }

    @Override
    public void blogCommentDelete(Integer blogUid) {
        //查出某博客下所有评论的uid
        List<Integer> commentsId = commentDao.blogCommentQuery(blogUid);
        //逐条删除
        for(Integer commentId:commentsId){
            //本类的方法
            deleteComment(commentId);
        }
    }
}
